<?php
namespace app\xhmmp\controller\admin;
use app\BaseController;
// use app\xhmmp\helper\upload\UploadHelper;
use app\xhmmp\model\ImgModel;

use COSSTS\Sts;

use app\common\logic\DataSettingLogic;
use Qiniu\Auth;


use think\Exception;
class Upload extends BaseController{
   
    public function add(){
        $from_type = $this->request->param('from_type/s');
        $type = $this->request->param('type/d');
        $url = $this->request->param('url/s');
        $size = $this->request->param('size/d');
        $ext = $this->request->param('ext/s');
        $key = $this->request->param('key/s');
        $hash = $this->request->param('hash/s');
        $name = $this->request->param('name/s');
        
        $ImgModel = new ImgModel();
        $ImgModel->shop_id = $this->request->shopId;
        $ImgModel->from_type= $from_type;
        $ImgModel->type= $type;
        $ImgModel->url= $url;
        $ImgModel->size= round($size/1024,2);
        $ImgModel->ext=$ext;
        $ImgModel->hash=$hash;
        $ImgModel->key=$key;
        $ImgModel->name=$name;
        if($ImgModel->save()){
            return  $this->success('上传成功');
        }else{
            return  $this->error('上传失败');
        }
    }
    
    public function getList(){
        $where = [
            ['shop_id','=', $this->request->shopId],
            ['is_delete','=',0]
        ];
        $count = ImgModel::where($where)->count();
        $datas = ImgModel::where($where)->limit($this->request
                ->limit)->page($this->request->page)->order([
                    'img_id' => 'desc'
                ])->select();
        return $this->success([
            'shopId' =>$this->request->shopId,
            'total' => $count,
            'datas' => $datas,
        ]);
    }
    public function del(){
        $imgId = $this->request->param('imgId/d');
        if(empty($imgId)){
            return $this->error('请选择要删除的图片');
        }
        if(null === $img = ImgModel::find($imgId)){
            return $this->error('请选择要删除的图片');  
        }
        try{
            $img->is_delete = 1;
            $img->save();
            return $this->success('删除成功');
        } catch (Exception $e){
            return  $this->error($e->getMessage());
        }
        
    }
    public function getQiniuToken(){
        if($this->request->accountId != 56 && $this->request->accountId != 1 && $this->request->accountId != 95 && $this->request->accountId != 115){
             return $this->error('无权限'.$this->request->accountId);
        }
        $DataSettingLogic = new DataSettingLogic();
        $config = $DataSettingLogic->getData('oss');
        
        $auth = new Auth(trim($config['qiniu']['ak']), trim($config['qiniu']['sk']));
        $token = $auth->uploadToken(trim($config['qiniu']['bucket']));
        return $this->success($token);
        // npm install qiniu-js 安装
        // import * as qiniu from 'qiniu-js' 引入
        // //file上传文件
        // //filename 上传至七牛云的文件名称要与上面提到的生成token的文件名称一致
        // //token 上传token 一般向后端获取
        // //putExtra 额外的参数，详情请参考文档
        // //config 配置信息 ，详情请参考文档
        // qiniu.upload(file, filename, token, putExtra, config);

    }
}
